<?php

namespace Biz\service\advertisement;

use Biz\cache\advertisement\CApiCounter;
use Doraemon\pockets\constInterface\Position\PositionRulesConstInterface;
use Doraemon\service\position\UserProfileModule\Action;

trait AdCounterManagement
{

    use AdCacheManagement;

    protected static $ad_storage = [];
    /**
     * 清除缓存
     * todo
     */
    public function clearApiCount($uid = 0, $debug = false)
    {
        $list = self::positionList();
        $delKeys = [];
        foreach ($list as $position_id => $v) {
            $name = $v['en_name'];
            $onceKey = $this->onceKey($uid, $position_id, 0);

            if (isset(self::$ad_storage[$name])) {
                $ad = self::$ad_storage[$name];
            } else {
                $ad = AdApi::debug($name, AdApi::PROCESS_LIST ^ AdApi::PROCESS_READ_CACHE);
                $ad = $ad['response'] ?? [];
                self::$ad_storage[$name] = $ad;
            }

            $ids = [];
            foreach ($ad as $vv) {
                $rules = $vv['_position_rules'] ?? [];
                $counterRules = $rules[PositionRulesConstInterface::RULE_TYPE_ACTION] ?? [];
                if ($counterRules && $counterRules['is_all'] == Action::PART_ONCE) {
                    $ids[] = $vv['id'];
                }
            }
            $all = CApiCounter::hGetAll($onceKey);
            if ($ids) {
                $keys = array_keys($all);
                $hashKeys = array_diff($keys, $ids);
                $top = array_shift($hashKeys);
                CApiCounter::hDel($onceKey, $top, ...$hashKeys);
            }

            if ($debug) echo $position_id, " $onceKey ", $v['title'], " count: ", count($ad) ," ids: ", (empty($ids) ? 'none' : implode(',', $ids)), " cache: ", json_encode($all), PHP_EOL;

            //todo empty($ids) 直接删除
            if (empty($ids)) $delKeys[] = $onceKey;
        }

        if ($delKeys) echo $uid , " del " , CApiCounter::del(...$delKeys), PHP_EOL;
    }



    /**
     * api调用次数验证使用
    */
    public function readApiCounter($counterRules, $uid, $position_id, $id)
    {
        if (empty($counterRules)) return true;

        $from = in_array($counterRules['is_all'], [Action::PART_DAY, Action::PART_START]) ? $this->writeFrom() : $this->readFrom();

        $type = Action::getApiCountKey($counterRules['is_all']);

        return $this->getApiCounterTypeValue($type, $uid, $position_id, $id, $from);
    }

    /**
     * 调用次数记录
     */
    public function recordApiCounter($counterRules, $uid, $position_id, $id)
    {
        if (empty($counterRules)) return true;

        if (in_array($counterRules['is_all'], [Action::PART_DAY, Action::PART_START])) return true;

        $from = $this->writeFrom();

        $type = Action::getApiCountKey($counterRules['is_all']);

        return $this->getApiCounterTypeValue($type, $uid, $position_id, $id, $from);
    }

    /**
     * 调用次数校验
     */
    public function countValid($rule, $time = null): bool
    {
        $hit = true;
        if (!empty($rule)) {
            switch ($rule['is_all']) {
                case Action::PART_DAY:
                case Action::PART_START:
                    $value = $rule['value'] ?? [];
                    $hit = in_array($time, $value);
                    break;
                default:
                    $hit = is_null($time);
                    break;
            }
        }
        return $hit;
    }


}